import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Stroke;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import nba2kmodtool.filedesc.N2k3DChildModelPart;
import nba2kmodtool.filedesc.N2k3DModelPart;

public class DrawFrame extends JFrame {

	public static void main(String[] args) {
		try {
			DrawFrame frame = new DrawFrame();
			frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}

	private static final long serialVersionUID = 1L;

	public DrawFrame() {
		this.setTitle("Drawing Frame");
		initComponents();
	}

	public void loadModel(N2k3DModelPart model) {
		this.model = model;
		int count = model.getChildNum();
		subSel.removeAllItems();
		for (int i = 0; i < count; i++) {
			subSel.addItem("Part: " + i);
		}
		subSel.setSelectedIndex(0);
	}

	private void subSelItemStateChanged(java.awt.event.ItemEvent evt) {
		if (subSel.getSelectedIndex() == -1) {
			return;
		}
		int index = subSel.getSelectedIndex();
		N2k3DChildModelPart childs[] = model.getChildParts();
		N2k3DChildModelPart child = childs[index];
		float[] uvs = child.getUvCoords().array();
		int[] coordsIndex = child.getCoordsIndex().array();
		drawingPanel.points = uvs;
		drawingPanel.edgesIndex = coordsIndex;
		drawingPanel.updateUI();
	}

	public void drawGraphic() {
		throw new UnsupportedOperationException("Draw Graphic");
	}

	public void drawPolygon(Graphics2D graphics, float[] points, int[] edgesIndex, int edgeCount) {
		graphics.clearRect(0, 0, DRAW_WIDTH, DRAW_HEIGHT);
		Stroke stroke = new BasicStroke(1.4F);
		graphics.setStroke(stroke);
		int indexCount = edgesIndex.length / edgeCount;
		for (int i = 0; i < (indexCount * edgeCount); i = i + 0) {
			Polygon polygon = new Polygon();
			for (int j = 0; j < edgeCount; j++) {
				int index = edgesIndex[i++] * 2;
				float x = points[index];
				float y = points[index + 1];
				y = 1 - y;
				if (Math.abs(x) < 1) {
					x *= DRAW_WIDTH;
				}
				if (Math.abs(y) < 1) {
					y *= DRAW_WIDTH;
				}
				polygon.addPoint((int) x, (int) y);
				graphics.draw(polygon);
			}
		}
	}

	private void initComponents() {
		this.setResizable(false);
		JPanel mainPanel = new JPanel();
		leftPanel = new JPanel();
		leftPanel.setPreferredSize(new Dimension(100, 400));
		subSel = new JComboBox();
		subSel.setPreferredSize(new Dimension(100, 25));
		subSel.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent evt) {
				subSelItemStateChanged(evt);
			}
		});
		leftPanel.add(subSel);
		mainPanel.add(leftPanel);
		drawingPanel = new DrawPanel();
		drawingPanel.setPreferredSize(new Dimension(DRAW_WIDTH, DRAW_HEIGHT));
		mainPanel.add(drawingPanel);
		add(mainPanel);
		pack();
	}

	private JPanel leftPanel;
	private DrawPanel drawingPanel;
	private N2k3DModelPart model;
	private JComboBox subSel;
	private static final int DRAW_WIDTH = 400;
	private static final int DRAW_HEIGHT = 400;

	class DrawPanel extends JPanel {
		private static final long serialVersionUID = 1L;

		public void paint(Graphics g) {
			Graphics2D graphics = (Graphics2D) g;
			if (points != null) {
				drawPolygon(graphics, points, edgesIndex, 3);
			}
		}

		private float[] points = null;
		private int[] edgesIndex = null;
	}
}
